package handler.wx

import service.wx.WxApi
import service.wx.WxPay
import service.wx.WxSignature
import service.wx.WxUserLocal

handler.post('/wx/pay/bige/order'){req, resp ->
req.jsonHandler{json ->
	String openid = json.openid
	log.info(openid)
	if(!openid){
		resp.json([flag: false, msg: 'no openid'])
		return
	}

	String type = json.type ?: '1'
	if(!['1', '2', '3'].contains(type))
		type = '1'

	// 以分为单位
	Map fees = ['1': 100, '2': 500, '3': 1000]
	int totalFee = fees[type]

	String body = json.body
	if(!body)
		body = '打赏我的。。。'

	def params = [:]

	params.openid = openid
	params.totalFee = totalFee
	params.body = body
	// 32位交易号
	params.tradeNo = WxPay.generateNonce()
	// 随便一个ip
	params.ip = '8.8.8.8'

	def r = WxPay.genPostParams(params)
	String prepayId = WxPay.getPrepayId(r)

	if(!prepayId){
		resp.json([flag: false, msg: 'create order failed'])
		return
	}

	def jsPayParams = WxPay.genPayJsParams(prepayId)
	jsPayParams.flag = true
	resp.json(jsPayParams)
}
}

handler.get('/wx/pay/bige-auth'){req, resp ->
	String openidTarget = req.params.openidTarget ?: ''
	String redirectUri = WxSignature.conf.urlHost + '/wx/pay/bige?showwxpaytitle=1&openidTarget=' + openidTarget

	if(req.params.openid){
		resp.redirect(redirectUri + '&openid=' + req.params.openid)
	}else{
		String appid = WxSignature.conf.appid
		redirectUri = URLEncoder.encode(redirectUri)

		String weixinOAuthUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
		resp.redirect(weixinOAuthUrl)
	}
}

handler.get('/wx/pay/bige'){req, resp ->
	String openidTarget = req.params.openidTarget ?: 'oQMltt6f8dgCfkst6T5f-T7BEawk'

	def userTarget = WxUserLocal.getUserCached(openidTarget)
	if(userTarget.errcode){
		log.warn('openid target get user failed - ' + openidTarget)
		resp.render('/wx/pay-test/bige-error.html', userTarget)
		return
	}

	String openid = req.params.openid
	if(openid){
		def user = WxUserLocal.getUserCached(openid)
		if(user.errcode){
			resp.render('/wx/pay-test/bige-error.html', user)
			return
		}

		Map r = [:]
		r.user = user
		r.userTarget = userTarget

		Map jsapiInfo = WxSignature.getJsapiInfo(WxSignature.conf.urlHost + req.uri)
		r.putAll(jsapiInfo)

		resp.render('/wx/pay-test/bige.html', r)
		return
	}

	// 没有openid，就应该是从oauth2跳转过来的
	String code = req.params.code
	if(code){
		def info = WxSignature.getAccessTokenInfo4Auth(code)
		if(info.errcode){
			resp.render('/wx/pay-test/bige-error.html', info)
			return
		}

		def user = WxApi.getUserInfo4Auth(info.access_token, info.openid)
		if(user.errcode){
			resp.render('/wx/pay-test/bige-error.html', user)
			return
		}

		Map r = [:]
		r.user = user
		r.userTarget = userTarget

		Map jsapiInfo = WxSignature.getJsapiInfo(WxSignature.conf.urlHost + req.uri)
		r.putAll(jsapiInfo)

		resp.render('/wx/pay-test/bige.html', r)
	}else{
		resp.render('/wx/pay-test/bige-reject.html', [:])
	}
}